package com.dm.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dm.util.VariantUtil;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Variant;

public class WindowServiceImpl implements WindowService {

	private static final String CLIENT_TO_SCREEN = "ClientToScreen";
	private static final String ENUM_WINDOW = "EnumWindow";
	private static final String ENUM_WINDOW_BYPROCESS = "EnumWindowByProcess";
	private static final String enum_window_super = "EnumWindowSuper";
	private static final String find_window = "FindWindow";
	private static final String find_window_by_process = "FindWindowByProcess";
	private static final String find_window_by_precessId = "FindWindowByProcessId";
	private static final String find_window_ex = "FindWindowEx";
	private static final String find_window_super = "FindWindowSuper";
	private static final String get_client_rect = "GetClientRect";
	private static final String get_client_size = "GetClientSize";
	private static final String get_foreground_focus = "GetForegroundFocus";
	private static final String get_foreground_window = "GetForegroundWindow";
	private static final String get_mouse_point_window = "GetMousePointWindow";
	private static final String get_point_window = "GetPointWindow";
	private static final String get_special_window = "GetSpecialWindow";
	private static final String get_window = "GetWindow";
	private static final String get_window_class = "GetWindowClass";
	private static final String get_window_process_id = "GetWindowProcessId";
	private static final String get_window_process_path = "GetWindowProcessPath";
	private static final String get_window_rect = "GetWindowRect";
	private static final String get_window_state = "GetWindowState";
	private static final String get_window_title = "GetWindowTitle";
	private static final String move_window = "MoveWindow";
	private static final String screen_to_client = "ScreenToClient";
	private static final String send_paste = "SendPaste";
	private static final String send_string = "SendString";
	private static final String set_client_size = "SetClientSize";
	private static final String set_window_size = "SetWindSize";
	private static final String set_window_text = "SetWindowText";
	private static final String set_window_state = "SetWindowState";
	private static final String set_window_transparent = "SetWindowTransoarent";
	private Logger log = LoggerFactory.getLogger(WindowServiceImpl.class);
	
	@Override
	public long clientToScreen(int hwnd, int x, int y, ActiveXComponent acComponent) {
		Variant[] variants = VariantUtil.varToVariants(hwnd,x,y);
		Variant result = acComponent.invoke(CLIENT_TO_SCREEN, variants);
		return result.getLong();
	}

	@Override
	public String enumWindow(int parent, String title, String className, int filter, ActiveXComponent acComponent) {
		Variant[] variants = VariantUtil.varToVariants(parent,title,className,filter);
		Variant result = acComponent.invoke(ENUM_WINDOW, variants);
		return result.getString();
	}

	@Override
	public String enumWindowByProcess(String processName, String title, String className, int filter,
			ActiveXComponent acComponent) {
		Variant[] variants = VariantUtil.varToVariants(processName,title,className);
		Variant result = acComponent.invoke(ENUM_WINDOW_BYPROCESS, variants);
		return result.getString();
	}

	@Override
	public String enumWindowSuper(String spec1, int flag1, int type1, String spec2, int flag2, int type2, int sort,
			ActiveXComponent acComponent) {
		Variant[] variants = VariantUtil.varToVariants(spec1,flag1,type1,spec2,flag2,type2,sort);
		Variant result = acComponent.invoke(enum_window_super, variants);
		return result.getString();
	}

	@Override
	public int findWindow(String className, String title, ActiveXComponent acComponent) {
		Variant[] variants = VariantUtil.varToVariants(className, title);
		Variant result = acComponent.invoke(find_window, variants);
		return result.getInt();
	}

	@Override
	public long findWindowByProcess(String processName, String className, String title, ActiveXComponent acComponent) {
		Variant[] variants = VariantUtil.varToVariants(processName, className, title);
		Variant result = acComponent.invoke(find_window_by_process, variants);
		return result.getLong();
	}

	@Override
	public long findWindowByProcessId(String processId, String className, String title, ActiveXComponent acComponent) {
		Variant[] variants = VariantUtil.varToVariants(processId, className, title);
		Variant result = acComponent.invoke(find_window_by_precessId, variants);
		return result.getLong();
	}

	@Override
	public long findWindowEx(int parent, String className, String title, ActiveXComponent acComponent) {
		Variant[] variants = VariantUtil.varToVariants(parent,className, title);
		Variant result = acComponent.invoke(find_window_ex, variants);
		return result.getLong();
	}

	@Override
	public long findWindowSuper(String spec1, int flag1, int type1, String spec2, int flag2, int type2,
			ActiveXComponent acComponent) {
		Variant[] variants = VariantUtil.varToVariants(spec1,flag1,type1,spec2,flag2,type2);
		Variant result = acComponent.invoke(find_window_super, variants);
		return result.getLong();
	}

	@Override
	public long getClientRect(int hwnd, int x1, int y1, int x2, int y2, ActiveXComponent acComponent) {
		Variant[] variants = VariantUtil.varToVariants(hwnd,x1,y1,x2,y2);
		Variant result = acComponent.invoke(get_client_rect, variants);
		return result.getLong();
	}

	@Override
	public long getClientSize(int hwnd, int width, int height, ActiveXComponent acComponent) {
		Variant[] variants = VariantUtil.varToVariants(hwnd, width, height);
		Variant result = acComponent.invoke(get_client_size, variants);
		return result.getLong();
	}

	@Override
	public long getForegroundFocus(ActiveXComponent acComponent) {
		Variant result = acComponent.invoke(get_foreground_focus);
		return result.getLong();
	}

	@Override
	public long getForegroundWindow(ActiveXComponent acComponent) {
		Variant result = acComponent.invoke(get_foreground_window);
		return result.getLong();
	}

	@Override
	public long getMousePointWindow(ActiveXComponent acComponent) {
		Variant result = acComponent.invoke(get_mouse_point_window);
		return result.getLong();
	}

	@Override
	public long getPointWindow(int x, int y, ActiveXComponent acComponent) {
		Variant[] variants = VariantUtil.varToVariants(x, y);
		Variant result = acComponent.invoke(get_point_window, variants);
		return result.getLong();
	}

	@Override
	public long getSpecialWindow(int flag, ActiveXComponent acComponent) {
		Variant[] variants = VariantUtil.varToVariants(flag);
		Variant result = acComponent.invoke(get_special_window, variants);
		return result.getLong();
	}

	@Override
	public long getWindow(int hwnd, int flag, ActiveXComponent acComponent) {
		Variant[] variants = VariantUtil.varToVariants(hwnd, flag);
		Variant result = acComponent.invoke(get_window, variants);
		return result.getLong();
	}

	@Override
	public String getWindowClass(int hwnd, ActiveXComponent acComponent) {
		Variant[] variants = VariantUtil.varToVariants(hwnd);
		Variant result = acComponent.invoke(get_window_class, variants);
		return result.getString();
	}

	@Override
	public long getWindowProcessId(int hwnd, ActiveXComponent acComponent) {
		Variant[] variants = VariantUtil.varToVariants(hwnd);
		Variant result = acComponent.invoke(get_window_process_id, variants);
		return result.getLong();
	}

	@Override
	public String getWindowProcessPath(int hwnd, ActiveXComponent acComponent) {
		Variant[] variants = VariantUtil.varToVariants(hwnd);
		Variant result = acComponent.invoke(get_window_process_path, variants);
		return result.getString();
	}

	@Override
	public long getWindowRect(int hwnd, int x1, int y1, int x2, int y2, ActiveXComponent acComponent) {
		Variant[] variants = VariantUtil.varToVariants(hwnd, x1, y1, x2, y2);
		Variant result = acComponent.invoke(get_window_rect, variants);
		return result.getLong();
	}

	@Override
	public long getWindowState(int hwnd, int flag, ActiveXComponent acComponent) {
		Variant[] variants = VariantUtil.varToVariants(hwnd, flag);
		Variant result = acComponent.invoke(get_window_state, variants);
		return result.getLong();
	}

	@Override
	public String getWindowTitle(int hwnd, ActiveXComponent acComponent) {
		Variant[] variants = VariantUtil.varToVariants(hwnd);
		Variant result = acComponent.invoke(get_window_title, variants);
		return result.getString();
	}

	@Override
	public long moveWindow(int hwnd, int x, int y, ActiveXComponent acComponent) {
		Variant[] variants = VariantUtil.varToVariants(hwnd,x,y);
		Variant result = acComponent.invoke(move_window, variants);
		String method = move_window;
		log.debug(method+": {},{},{} and result:{}", hwnd,x,y,result);
		return result.getInt();
	}

	@Override
	public long screenToClient(int hwnd, int x, int y, ActiveXComponent acComponent) {
		Variant[] variants = VariantUtil.varToVariants(hwnd,x,y);
		Variant result = acComponent.invoke(screen_to_client, variants);
		return result.getLong();
	}

	@Override
	public long sendPaste(int hwnd, ActiveXComponent acComponent) {
		Variant[] variants = VariantUtil.varToVariants(hwnd);
		Variant result = acComponent.invoke(send_paste, variants);
		return result.getLong();
	}

	@Override
	public long sendString(int hwnd, String str, ActiveXComponent acComponent) {
		Variant[] variants = VariantUtil.varToVariants(hwnd,str);
		Variant result = acComponent.invoke(send_string, variants);
		String method = send_string;
		log.debug(method+": {},{},and result:{}", hwnd,str,result);
		return result.getInt();
	}

	@Override
	public long setClientSize(int hwnd, int width, int height, ActiveXComponent acComponent) {
		Variant[] variants = VariantUtil.varToVariants(hwnd,width, height);
		Variant result = acComponent.invoke(set_client_size, variants);
		return result.getLong();
	}

	@Override
	public long setWindowSize(int hwnd, int width, int height, ActiveXComponent acComponent) {
		Variant[] variants = VariantUtil.varToVariants(hwnd,width, height);
		Variant result = acComponent.invoke(set_window_size, variants);
		return result.getLong();
	}

	@Override
	public long setWindowState(int hwnd, int flag, ActiveXComponent acComponent) {
		Variant[] variants = VariantUtil.varToVariants(hwnd,flag);
		Variant result = acComponent.invoke(set_window_state, variants);
		return result.getInt();
	}

	@Override
	public long setWindowText(int hwnd, int title, ActiveXComponent acComponent) {
		Variant[] variants = VariantUtil.varToVariants(hwnd,title);
		Variant result = acComponent.invoke(set_window_text, variants);
		return result.getLong();
	}

	@Override
	public long setWindowTransparent(int hwnd, int trans, ActiveXComponent acComponent) {
		Variant[] variants = VariantUtil.varToVariants(hwnd,trans);
		Variant result = acComponent.invoke(set_window_transparent, variants);
		return result.getLong();
	}
}
